package com.globallogic.dmytro_zakharenko.university;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

import com.globallogic.dmytro_zakharenko.university.lessons.Lesson;
import com.globallogic.dmytro_zakharenko.university.person.Student;
import com.globallogic.dmytro_zakharenko.university.person.Teacher;

public class Course {
	
	private ArrayList<Lesson> mCourseLessons;
	private ArrayList<String> mStudentsName;
	private Set<Student> mEnrolledStudents;
	private Set<Teacher> mEnrolledTeachers;
	
	/**
     * Constructor
     *
     */
	public Course(ArrayList<Lesson> lessonsName) {
		if (lessonsName != null) {
			this.setLessonsName(lessonsName);
		} else {
			throw new RuntimeException("No Lessons were setted. Please set Lessons when create Course obj.");
		}
	}

	/**
     * returns course lessons
     */
	public  ArrayList<Lesson> getLessonsOfCourse() {
		return mCourseLessons;
	}

	/**
     * set course lessons
     */
	private void setLessonsName(ArrayList<Lesson> lessonsName) {
		this.mCourseLessons = lessonsName;
	}
	
	/**
     * set students for the current course 
     */
	public void enrollStudents(Set<Student> enrolledStudents) {
		this.mEnrolledStudents = enrolledStudents;
	}
	
	/**
     * return students for the current course 
     */
	public Set<Student> getEnrolledStudents() {
		return mEnrolledStudents;
	}
	
	/**
     * set teachers for the current course 
     */
	public void enrollTeachers(Set<Teacher> enrolledTeachers) {
		this.mEnrolledTeachers = enrolledTeachers;
	}
	
	/**
     *  return teachers for the current course 
     */
	public Set<Teacher> getEnrolledTeachers() {
		return mEnrolledTeachers;
	}
	
	/**
     *  return number of students for the current course 
     */
	public int getNumberOfStudents() {
		return mStudentsName.size();
	}
	
	/**
     *  return number of lessons for the current course 
     */
	public int getNumberOfLessons() {
		return mCourseLessons.size();
	}
	
	/**
     *  return teacher for the user specified lesson
     *  
     *  @param lessonsName, return null if no Teacher was found 
     */
	public Teacher getLessonTeacher(String lessonsName) {
		for (Teacher t : mEnrolledTeachers) {
			for (String ls : t.getLessons()) {
				if (lessonsName.equalsIgnoreCase(ls)) {
					return t;
				}
			}
		}
		
		return null;
	}
	
	public boolean equals(Object other) {
		if (this == other) {
			return true;
		} 
		if (!(other instanceof Course)) {
			return false;
		}
		
		Course otherA = (Course) other;
		
		return (Arrays.equals(mCourseLessons.toArray(new Lesson[0]), otherA.mCourseLessons.toArray(new Lesson[0])) 
				&& Arrays.equals(mEnrolledStudents.toArray(new Student[0]), otherA.mEnrolledStudents.toArray(new Student[0])));	
	}
	
	public int hashCode() {
		int result = 0;
		for (Lesson l : mCourseLessons) {
			result += l.hashCode(); 
		}
		
		if (mEnrolledStudents != null) {
			for (Student s : mEnrolledStudents) {
				result += s.hashCode(); 
			}
		}
		
		return result;
	}
	
	public String toString() {
		return ("The course have : " + "Number Of lessons: " +this.getNumberOfLessons() + "Number Of students: " + this.getNumberOfStudents());
	}
}
